<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 我的写法,有点垃圾,性能不行 -->
    <!-- <script>
      /**
       * 传入一个时间段,返回这个时间段同一小时时间段
       * 1. 0815 - 0915 不属于同一小时
       * **/
      function getTimeStr(str) {
        return String(Number(str) + 99).padStart(4, '0');
      }

      function isHour(str, str2) {
        return Number(str2) >= Number(str) && Number(str2) - Number(str) < 100;
      }

      function getTimeSS(arr) {
        let index = 0;
        let endTime = '';
        for (let i = 0; i < arr.length; i++) {
          const t = arr[i];
          const end = getTimeStr(t);
          const f = arr.filter((o) => isHour(o, end));
          if (f.length > index) {
            index = f.length;
            endTime = end;
          }
        }
        return [endTime, index];
      }

      function findHighAccessEmployees(access_times) {
        const obj = {};
        const result = [];
        for (const [name, t] of access_times) {
          if (!obj[name]) {
            obj[name] = [];
          }
          obj[name].push(t);
        }
        for (const key in obj) {
          if (Object.hasOwnProperty.call(obj, key)) {
            const element = obj[key];
            const arr = getTimeSS(element);
            if (arr[1] >= 3) {
              result.push(key);
            }
          }
        }
        return result;
      }

      console.log(
        findHighAccessEmployees([
          ['d', '0002'],
          ['c', '0808'],
          ['c', '0829'],
          ['e', '0215'],
          ['d', '1508'],
          ['d', '1444'],
          ['d', '1410'],
          ['c', '0809'],
        ])
      );
    </script> -->

    <!-- 大佬的写法,打败 100% 的人 -->
    <script>
      function findHighAccessEmployees(access_times) {
        const obj = {};
        const result = [];
        for (let i = 0; i < access_times.length; i++) {
          const name = access_times[i][0];
          const t = access_times[i][1];
          const time = Number(t.substring(0, 2)) * 60 + Number(t.substring(2));
          if (!obj[name]) {
            obj[name] = [];
          }
          obj[name].push(time);
        }
        for (const [name, values] of Object.entries(obj)) {
          values.sort();
          for (let i = 2; i < values.length; i++) {
            if (values[i] - values[i - 2] < 60) {
              result.push(name);
              break;
            }
          }
        }
        return result;
      }
      console.log(
        findHighAccessEmployees([
          ['d', '0002'],
          ['c', '0808'],
          ['c', '0829'],
          ['e', '0215'],
          ['d', '1508'],
          ['d', '1444'],
          ['d', '1410'],
          ['c', '0809'],
        ])
      );
    </script>
  </body>
</html>
